package com.sky.controller.admin;

import com.sky.annotation.AutoFill;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 通用接口
 */
@RestController
@Slf4j
@RequestMapping("/admin/common")
@Tag(name = "通用接口")
public class CommonController {

    @Autowired
    private AliOssUtil aliOssUtil;
    @PostMapping("/upload")
    public Result upload(MultipartFile file){
        log.info("upload file: {}",file);
        //重命名防止文件名冲突

        try {
            String originalname = file.getOriginalFilename();
            String extension = originalname.substring(originalname.lastIndexOf("."));
            String objectname = UUID.randomUUID().toString()+extension;
            String url = aliOssUtil.upload(file.getBytes(),objectname);
            return Result.success(url);
        } catch (IOException e) {
            log.error("upload file failed: {}",e);
        }
        return Result.error("文件上传失败");
    }
}
